﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using Model;

namespace TanHungLong
{
    public partial class FrmNgachBacLuong : DevExpress.XtraEditors.XtraForm
    {
        #region variant
        private FormStatus formStatus = FormStatus.View;
        private bool allowCloseForm = true;
        private NgachLuongBOCollection ngachLuongBOCollection = null;
        private BacLuongBOCollection bacLuongBOCollection = null;

        private string COLUMN_FIRST_NGACH = "Ten";
        private string COLUMN_SECOND_NGACH = "SoThuTu";

        private string COLUMN_FIRST_BAC = "Ten";
        private string COLUMN_SECOND_BAC = "HeSo";
        #endregion variant

        //=====================================
        #region form
        public FrmNgachBacLuong()
        {
            InitializeComponent();

            this.Load += new EventHandler(FrmNgachBacLuong_Load);
            this.FormClosing += new FormClosingEventHandler(FrmNgachBacLuong_FormClosing);

            this.btnRefresh.Click += new EventHandler(btnRefresh_Click);
            this.btnEdit.Click += new EventHandler(btnEdit_Click);
            this.btnSave.Click += new EventHandler(btnSave_Click);
            this.btnCancel.Click += new EventHandler(btnCancel_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);

            this.grdViewNgach.RowCellStyle += new RowCellStyleEventHandler(grdView_RowCellStyle);
            this.grdViewBac.RowCellStyle += new RowCellStyleEventHandler(grdView_RowCellStyle);

            this.grdViewNgach.ShowingEditor += new CancelEventHandler(grdView_ShowingEditor);
            this.grdViewBac.ShowingEditor += new CancelEventHandler(grdView_ShowingEditor);
        }

        private void FrmNgachBacLuong_Load(object sender, EventArgs e)
        {
            RefreshData();
            SetFormStatus(this.formStatus);

            this.grdViewNgach.FocusedRowChanged += new FocusedRowChangedEventHandler(grdViewNgach_FocusedRowChanged);
        }

        private void FrmNgachBacLuong_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.allowCloseForm == false)
                e.Cancel = true;
        }
        #endregion form

        //=====================================
        #region control
        #endregion control

        //=====================================
        #region button
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            Edit();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Cancel();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion button

        //=====================================
        #region grid
        private void grdViewNgach_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            FilterNgach();
        }

        private void grdView_ShowingEditor(object sender, CancelEventArgs e)
        {
            GridView grdView = (GridView)sender;

            bool del = StaticUtil.GetBool(grdView.GetFocusedRowCellValue("DELE"));
            string fieldName = grdView.FocusedColumn.FieldName;

            if (del && fieldName != "DELE")
                e.Cancel = true;
            else
                if (fieldName == "DELE" && grdView.FocusedRowHandle == GridControl.NewItemRowHandle)
                    e.Cancel = true;
        }

        private void grdView_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            bool del = StaticUtil.GetBool(this.grdViewNgach.GetRowCellValue(e.RowHandle, "DELE"));

            if (del)
            {
                e.Appearance.Font = MyStyle.DELETE_FONT;
                e.Appearance.ForeColor = MyStyle.DELETE_COLOR;
            }
        }
        #endregion grid

        //=====================================
        #region function
        private void FilterNgach()
        {
            Session.NgachLuongID = StaticUtil.GetRealInt(this.grdViewNgach.GetFocusedRowCellValue("NgachLuongID"));

            if (this.formStatus == FormStatus.Edit)
                this.grdViewBac.OptionsView.NewItemRowPosition = (Session.NgachLuongID != null ? NewItemRowPosition.Bottom : NewItemRowPosition.None);

            if (Session.NgachLuongID == null)
                this.grdViewBac.ActiveFilterString = "NgachLuongID=" + int.MaxValue;
            else
                this.grdViewBac.ActiveFilterString = "NgachLuongID=" + Session.NgachLuongID.Value.ToString();

            this.grdViewBac.CheckLoaded();
            SetSttBac();
        }

        private void RefreshData()
        {
            this.ngachLuongBOCollection = NgachLuongBOCollection.GetNgachLuongBOCollection("", "SoThuTu");
            this.bacLuongBOCollection = BacLuongBOCollection.GetBacLuongBOCollection("", "SoThuTu");

            this.bdSourceNgach.DataSource = this.ngachLuongBOCollection.Clone();
            this.bdSourceBac.DataSource = this.bacLuongBOCollection.Clone();

            FilterNgach();
        }

        private void Edit()
        {
            this.formStatus = FormStatus.Edit;
            SetFormStatus(this.formStatus);
            Session.CloseIndex++;
        }

        private void Save()
        {
            string oldColumn = this.grdViewNgach.FocusedColumn.FieldName;
            string newColumn = (oldColumn == this.COLUMN_FIRST_NGACH ? this.COLUMN_SECOND_NGACH : this.COLUMN_FIRST_NGACH);
            this.grdViewNgach.FocusedColumn = this.grdViewNgach.Columns[newColumn];

            string oldColumn2 = this.grdViewBac.FocusedColumn.FieldName;
            string newColumn2 = (oldColumn2 == this.COLUMN_FIRST_BAC ? this.COLUMN_SECOND_BAC : this.COLUMN_FIRST_BAC);
            this.grdViewBac.FocusedColumn = this.grdViewBac.Columns[newColumn2];

            this.bdSourceNgach.EndEdit();
            this.bdSourceBac.EndEdit();

            this.ngachLuongBOCollection = (NgachLuongBOCollection)this.bdSourceNgach.DataSource;
            this.bacLuongBOCollection = (BacLuongBOCollection)this.bdSourceBac.DataSource;

            this.ngachLuongBOCollection = this.ngachLuongBOCollection.Save();

            int i = 0;
            int length = this.bacLuongBOCollection.Count;
            int oldID = 0;
            for (i = 0; i < length; i++)
            {
                oldID = this.bacLuongBOCollection[i].NgachLuongID.Value;
                if (this.ngachLuongBOCollection.hstID.ContainsKey(oldID))
                    this.bacLuongBOCollection[i].NgachLuongID = (int)this.ngachLuongBOCollection.hstID[oldID];
                else //không có chứa trong danh sách mới tức ngạch đã bị xóa
                    this.bacLuongBOCollection[i].DELE = true;
            }
            this.bacLuongBOCollection = this.bacLuongBOCollection.Save();


            this.bdSourceNgach.DataSource = this.ngachLuongBOCollection.Clone();
            this.bdSourceBac.DataSource = this.bacLuongBOCollection.Clone();

            this.grdViewNgach.FocusedColumn = this.grdViewNgach.Columns[oldColumn];
            this.grdViewBac.FocusedColumn = this.grdViewBac.Columns[oldColumn2];

            this.formStatus = FormStatus.View;
            SetFormStatus(this.formStatus);
            Session.CloseIndex--;

            FilterNgach();
        }

        private void Cancel()
        {
            this.formStatus = FormStatus.View;
            SetFormStatus(this.formStatus);
            Session.CloseIndex--;

            this.bdSourceNgach.DataSource = this.ngachLuongBOCollection.Clone();
            this.bdSourceBac.DataSource = this.bacLuongBOCollection.Clone();

            FilterNgach();
        }

        private void SetFormStatus(FormStatus formStatus)
        {
            bool edit = (formStatus != FormStatus.View);
            this.allowCloseForm = !edit;

            MyStyle.SetGridStyle(this.grdViewNgach, edit);
            MyStyle.SetGridStyle(this.grdViewBac, edit);

            this.btnEdit.Visible = !edit;
            this.btnClose.Visible = !edit;
            this.btnRefresh.Visible = !edit;

            this.btnSave.Visible = edit;
            this.btnCancel.Visible = edit;

            this.colDELE_Ngach.Visible = edit;
            foreach (GridColumn col in this.grdViewNgach.Columns)
            {
                if (col.Visible)
                    col.OptionsColumn.AllowEdit = edit;
            }

            this.colDELE_Bac.Visible = edit;
            foreach (GridColumn col in this.grdViewBac.Columns)
            {
                if (col.Visible)
                    col.OptionsColumn.AllowEdit = edit;
            }


            this.grdViewNgach.OptionsView.NewItemRowPosition = (edit ? NewItemRowPosition.Bottom : NewItemRowPosition.None);

            if (edit)
                this.grdViewBac.OptionsView.NewItemRowPosition = (Session.NgachLuongID != null ? NewItemRowPosition.Bottom : NewItemRowPosition.None);
            else
                this.grdViewBac.OptionsView.NewItemRowPosition = NewItemRowPosition.None;

            this.grdViewNgach.Focus();

            if (edit)
            {
                string focusColumn = this.grdViewNgach.FocusedColumn.FieldName;

                if (string.IsNullOrEmpty(focusColumn))
                    focusColumn = this.COLUMN_FIRST_NGACH;
                else
                    if (focusColumn == "DELE")
                        focusColumn = this.COLUMN_FIRST_NGACH;

                if (this.grdViewNgach.RowCount == 1)
                    this.grdViewNgach.FocusedRowHandle = int.MinValue + 1;

                this.grdViewNgach.FocusedColumn = this.grdViewNgach.Columns[focusColumn];
                this.grdViewNgach.ShowEditor();
            }
        }

        private void SetSttBac()
        {
            if (this.formStatus == FormStatus.Edit)
                this.bdSourceBac.EndEdit();

            int stt = 0;
            int length = this.grdViewBac.RowCount;
            int index = 0;
            for (int i = 0; i < length; i++)
            {
                index = this.grdViewBac.GetVisibleRowHandle(i);
                if (index >= 0)
                    this.grdViewBac.SetRowCellValue(i, "SoThuTu", ++stt);
            }

            ((BacLuongBOCollection)this.bdSourceBac.DataSource).SttIndex = stt;
        }
        #endregion function
    }
}